﻿using DataAccess;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace KhoaLuan.MainSys.Controls
{
    public partial class ctArticleByOrgPaging : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            var proxy = new tbPosts_Proxy();
            var orgid = Request.QueryString["OrgID"];
            var page = Request.QueryString["Page"];
            int _orgid;
            if (orgid != null)
                _orgid = int.Parse(orgid);
            else
                _orgid = 1;
            int _page;
            if (page != null)
                _page = int.Parse(page);
            else
                _page = 1;
            repBaiViet.ItemDataBound += repBaiViet_ItemDataBound;
            repBaiViet.DataSource = proxy.SelectNewsByOrg(_orgid);
            repBaiViet.DataBind();
        }

        void repBaiViet_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var tbpost = e.Item.DataItem as tbPosts;
            var lbdonvi = e.Item.FindControl("lbDonVi") as System.Web.UI.HtmlControls.HtmlGenericControl;
            if (lbdonvi != null)
            {
                var proxyorg = new tbOrg_Proxy();
                var org = proxyorg.GetById_tbOrg_Proxy(tbpost.OrgID).ToObject<tbOrg>();
                if (org != null)
                    lbdonvi.InnerHtml = string.Format("Đơn vị: {0}", org.NameOrg);
            }
            var lbdate = e.Item.FindControl("lbDate") as System.Web.UI.HtmlControls.HtmlGenericControl;
            if (lbdate != null)
                lbdate.InnerHtml = string.Format("Ngày {0} tháng {1} năm {2}", tbpost.CreatedDate.Day, tbpost.CreatedDate.Month, tbpost.CreatedDate.Year);
            var img = e.Item.FindControl("imgHinhMoTa") as System.Web.UI.HtmlControls.HtmlImage;
            if (tbpost.ImagePath == null)
                img.Visible = false;
            else
                img.Src = tbpost.ImagePath.Replace("~", "");
        }
    }
}